package org.example.myleet.p1629;

public class Solution {
    public char slowestKey(int[] releaseTimes, String keysPressed) {
        int[] keyPressTime = new int[128];
        int n = keysPressed.length();
        int time = 0;
        char[] charArr = keysPressed.toCharArray();
        char candidateKey = 'a';
        int maxTime = Integer.MIN_VALUE;
        for (int i = 0; i < n; ++i) {
            char c = charArr[i];
            keyPressTime[c] = Math.max(keyPressTime[c], releaseTimes[i] - time);
            time = releaseTimes[i];
            if (maxTime < keyPressTime[c] || (maxTime == keyPressTime[c] && candidateKey < c)) {
                maxTime = keyPressTime[c];
                candidateKey = c;
            }
        }
        return candidateKey;
    }
}
